<?php

namespace Admin\Controller;

use Think\Controller;

class AntiUserController extends CommonController
{

    public function antiList()
    {
        $this->display();
    }
    /**
     * 疯狂打电话
     */
    public function call()
    {
        $times = date('YmdHis', time());
        $phone = I('phone');
        if(company_test($phone)){
            $this->ajax('1002', '内部测试人员');
            die;
        }

        $accountID = 'N00000020241';
        $APISecret = 'b9e14b50-2d7a-11e8-970c-995458c2f812';
        $number = $accountID . $APISecret . $times;
        $sig_s = md5($number);
        $sig = strtoupper($sig_s);
        $url = 'http://apis.7moor.com/v20160818/call/dialout/N00000020241?sig=' . $sig;
//        $url = 'http://apis.7moor.com/v20160818/call/dialout/N00000017174?sig=' . $sig;
        $agent = M('admin', '', 'KEFU')->where(['id' => $_SESSION['id']])->getField('agent');
        $FromExten = substr($agent, 0, 4);
        $post_data = array('FromExten' => $FromExten, 'Exten' => $phone, 'ExtenType' => 'gateway');
        $post_data = json_encode($post_data);

//        $str = 'N00000017174:' . $times;
        $str = 'N00000020241:' . $times;
        $baseStr = base64_encode($str);
        //接口鉴权
        $header = array("Accept:application/json", "Content-Type:application/json;charset=utf-8", "Content-Length:" . strlen($post_data), "Authorization:" . $baseStr);
        $ret = $this->http($url, $post_data, 1, $header);
        $data = [];
        $data['ret'] = $ret;
        $data['url'] = $url;
        $data['header'] = $header;
        $data['post_data'] = $post_data;
        $msg = json_decode($ret, true);
        if ($msg['Succeed'] == false) {
            $this->ajax('1002', $msg['Message']);
        } else {
//             echo $msg1;
            $this->ajaxReturn(array('code'=>'1001','action_id'=>$msg['ActionID']));
//            $this->ajax('1001', $msg1);
        }
        die;

//        账户编号：	N00000017174
//        帐号APISecret：	1b4c7a40-d664-11e7-acd3-d9bc7f04577f
//
//    PBX   	地址
//hz.ali.1.4	http://120.26.127.74

    }

    /**
     * 反欺诈用户列表  待查
     */
    public function anti_user_list1()
    {
        $n = I('page');
        $num = 10;
        $page = ($n - 1) * $num;
        $where = [];
        //普通客服只能看见分配给自己的用户
        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '11') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
        }

        $where['b.status'] = array('in', array(0, 7, 8, 12));
        $where['a.status'] = array('not in', array(2, 3));//已经有结果的用户则过滤不显示
        $where['a.quota'] = array('eq', 10000);
        $count = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->count();
        $count = intval($count);
        $field = 'a.truename as name,a.mobile,a.sex,b.status,a.cert_time,a.vid,c.name as admin_name,b.id';
        $user = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join('dxd_kefu.admin c on b.out_kf_id=c.id')->where($where)->LIMIT($page, $num)->field($field)->select();
        foreach ($user as $k => $v) {
            //用户评分
            $user[$k]['logic_score'] = M('user_good_bad', '', 'USER')->where(['vid' => $v['vid']])->getfield('logic_level');

            if ($v['cert_time'] != 0) {
                $user[$k]['cert_time'] = date('Y-m-d H:i', $v['cert_time']);
            } else {
                $user[$k]['cert_time'] = '-';
            }

            if ($v['sex'] == 1) {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $user[$k]['sex'] = '女';
            } else {
                $user[$k]['sex'] = '未知';
            }

            if ($v['status'] == 0) {
                $user[$k]['status'] = '未开始';
            } elseif ($v['status'] == 12) {
                $user[$k]['status'] = '重审核';
            } else if ($v['status'] == 4) {
                $user[$k]['status'] = '审核驳回';
            } else if ($v['status'] == 7) {
                $user[$k]['status'] = '正在调查';
            } else if ($v['status'] == 8) {
                $user[$k]['status'] = '驳回';
            }
            //隐藏手机号
            $first = substr($v['mobile'], 0, 3);
            $end = substr($v['mobile'], 7, 4);
            $user[$k]['user_mobile'] = $first . '*****' . $end;
        }

        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $user]);

    }

    /**
     * 反欺诈用户列表  待查
     */
    public function anti_user_list()
    {
        $n = I('page');
        $num = 10;
        $page = ($n - 1) * $num;
        $where = [];
        //普通客服只能看见分配给自己的用户
        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '11') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
            $w['out_kf_id'] = array('eq', $_SESSION['id']);
        }

        $where['b.status'] = array('eq', 2);
        $w['status'] = array('eq', 2);
//        $where['a.status'] = array('not in', array(2,3));//已经有结果的用户则过滤不显示
//        $where['a.quota'] = array('eq',10000);
        $count = M('user_artificial_check', '', 'KEFU_R')->where($w)->count();
        $count = intval($count);
        $field = 'a.truename as name,a.mobile,a.sex,b.status,a.cert_time,a.vid,b.research_admin as admin_name,a.id,b.examine_admin,b.ready_time';
        $user = M('user_artificial_check', '', 'KEFU_R')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->LIMIT($page, $num)->field($field)->select();
        foreach ($user as $k => $v) {
            //用户评分
            $user[$k]['logic_score'] = M('user_good_bad', '', 'USER')->where(['vid' => $v['vid']])->getfield('logic_level');

            if ($v['cert_time'] != 0) {
                $user[$k]['cert_time'] = date('Y-m-d H:i', $v['cert_time']);
            } else {
                $user[$k]['cert_time'] = '-';
            }
            if ($v['ready_time']> 0) {
                $user[$k]['ready_time'] = date('Y-m-d H:i', $v['ready_time']);
            } else {
                $user[$k]['ready_time'] = '--';
            }
            if ($v['sex'] == 1) {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $user[$k]['sex'] = '女';
            } else {
                $user[$k]['sex'] = '未知';
            }


            if(!empty($v['examine_admin'])){
                $user[$k]['status'] = '审核驳回';
            }else{
                if ($v['status'] == 0) {
                    $user[$k]['status'] = '未开始';
                } elseif ($v['status'] == 2) {
                    $user[$k]['status'] = '调查中';
                }
            }



            //隐藏手机号
            $first = substr($v['mobile'], 0, 3);
            $end = substr($v['mobile'], 7, 4);
            $user[$k]['user_mobile'] = $first . '*****' . $end;
        }

        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $user]);

    }

    /**
     * 待审核审核列表
     */
    public function fk_check()
    {
        $n = I('page');
        $num = 10;
        $page = ($n - 1) * $num;
        $where['b.status'] = array('in', array('3', '4'));
        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '11') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
        }
        $sum = M('user_artificial_check', '', 'KEFU_R')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->distinct('a.vid')->field('a.vid')->select();
        $count = count($sum);
        $field = 'a.truename as name,a.mobile,a.sex,b.status,b.research_commit_time as commit_time,a.vid';
        $user = M('user_artificial_check', '', 'KEFU_R')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->LIMIT($page, $num)->order('b.research_commit_time desc')->field($field)->select();

        foreach ($user as $k => $v) {
            //用户评分
            $user[$k]['logic_score'] = M('user_good_bad', '', 'USER')->where(['vid' => $v['vid']])->getfield('logic_level');

            if ($v['sex'] == 1) {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $user[$k]['sex'] = '女';
            } else {
                $user[$k]['sex'] = '未知';
            }
            if ($v['commit_time'] != 0) {
                $user[$k]['commit_time'] = date('Y-m-d H:i', $v['commit_time']);
            } else {
                $user[$k]['commit_time'] = '-';
            }

            if ($v['status'] == '3') {
                $user[$k]['status'] = '待接收';
                $user[$k]['check_status'] = '1';

            } elseif ($v['status'] == '4') {
                $user[$k]['status'] = '审核中';
                $user[$k]['check_status'] = '2';
            }

            //隐藏手机号
            $first = substr($v['mobile'], 0, 3);
            $end = substr($v['mobile'], 7, 4);
            $user[$k]['user_mobile'] = $first . '*****' . $end;
        }
        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $user]);

    }

    /**
     * 待审核审核列表
     */
    public function fk_check1()
    {
        $n = I('page');
        $num = 10;
        $page = ($n - 1) * $num;
        $where['b.status'] = array('in', array('2', '3'));
        if ($_SESSION['role_id'] != '3' && $_SESSION['role_id'] != '11') {
            $where['b.out_kf_id'] = array('eq', $_SESSION['id']);
        }
        $sum = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->distinct('a.vid')->field('a.vid')->select();
        $count = count($sum);
        $field = 'a.truename as name,a.mobile,a.sex,b.status,b.commit_time,a.vid';
        $user = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->LIMIT($page, $num)->order('b.commit_time desc')->field($field)->select();

        foreach ($user as $k => $v) {
            //用户评分
            $user[$k]['logic_score'] = M('user_good_bad', '', 'USER')->where(['vid' => $v['vid']])->getfield('logic_level');

            if ($v['sex'] == 1) {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $user[$k]['sex'] = '女';
            } else {
                $user[$k]['sex'] = '未知';
            }
            if ($v['commit_time'] != 0) {
                $user[$k]['commit_time'] = date('Y-m-d H:i', $v['commit_time']);
            } else {
                $user[$k]['commit_time'] = '-';
            }

            if ($v['status'] == '2') {
                $user[$k]['status'] = '待接收';
                $user[$k]['check_status'] = '1';

            } elseif ($v['status'] == '3') {
                $user[$k]['status'] = '审核中';
                $user[$k]['check_status'] = '2';
            }

            //隐藏手机号
            $first = substr($v['mobile'], 0, 3);
            $end = substr($v['mobile'], 7, 4);
            $user[$k]['user_mobile'] = $first . '*****' . $end;
        }
        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $user]);

    }

    /**
     *  通过审核列表 未通过审核列表
     */
    public function pass_list()
    {
        $type = I('type');
        $n = I('page');
        $num = 10;
        $page = ($n - 1) * $num;
        $field = 'a.truename as name,a.mobile,a.sex,b.examine_over_time as check_time,a.vid,b.research_admin as admin_name';
        if ($type == 4) {//未通过审核
            $where['b.status'] = array('eq', 6);
            $w['status']=6;
        } else {//通过审核
            $where['b.status'] = array('eq', 5);
            $w['status']=5;
        }

//        $count = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where($where)->count("DISTINCT b.vid");
        $count= M('user_artificial_check', '', 'KEFU_R')->where($w)->count();
//        $user = M('anti_check_user', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->join(' left join dxd_kefu.admin c on b.out_kf_id=c.id')->where($where)->LIMIT($page, $num)->order('b.check_time desc')->field($field)->select();
       $user=M('user_artificial_check', '', 'KEFU_R')->join('b join dxd_user.user a on a.vid=b.vid')->where($where)->limit($page, $num)->order('b.examine_over_time desc')->field($field)->select();
        foreach ($user as $k => $v) {
            //用户评分
            $user[$k]['logic_score'] = M('user_good_bad', '', 'USER')->where(['vid' => $v['vid']])->getfield('logic_level');

            if ($v['sex'] == 1) {
                $user[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $user[$k]['sex'] = '女';
            } else {
                $user[$k]['sex'] = '未知';
            }
            if ($v['check_time'] != 0) {
                $user[$k]['check_time'] = date('Y-m-d H:i', $v['check_time']);
            } else {
                $user[$k]['check_time'] = '-';
            }
            //隐藏手机号
            $first = substr($v['mobile'], 0, 3);
            $end = substr($v['mobile'], 7, 4);
            $user[$k]['user_mobile'] = $first . '*****' . $end;
        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $user]);
    }

    /**
     *  开始调查用户详情页
     */
    public function survey()
    {
        $id = I('id');
//      $vid = I('get.vid');
//        $vid = M('anti_check_user', '', 'KEFU')->where(['id' => $id])->getField('vid');
        //修改调查状态
        $field = 'vid,mobile,sex,ctime,address,residence_time,id_card,shebao_verify,work_verify,taobao_verify,jd_verify,credit_verify,edu_verify,alipay_verify';
        $user = M('user', '', 'USER')->where(['id' => $id])->field($field)->find();
        $vid = $user['vid'];
        //用户各种认证
        // 社保认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['shebao_verify']) {
            case 1:
                $user['shebao_verify'] = '未认证';
                break;
            case 2:
                $user['shebao_verify'] = '认证中';
                break;
            case 3:
                $user['shebao_verify'] = '认证成功';
                break;
            case 4:
                $user['shebao_verify'] = '认证失败';
                break;
        }

        //京东认证1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['jd_verify']) {
            case 1:
                $user['jd_verify'] = '未认证';
                break;
            case 2:
                $user['jd_verify'] = '认证中';
                break;
            case 3:
                $user['jd_verify'] = '认证成功';
                break;
            case 4:
                $user['jd_verify'] = '认证失败';
                break;
        }

        //工作认证 1认证2未认证
        switch ($user['work_verify']) {
            case 1:
                $user['work_verify'] = '已认证';
                break;
            case 2:
                $user['work_verify'] = '待修改';
                break;
            case 0:
                $user['work_verify'] = '未认证';
                break;
        }

        //淘宝认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['taobao_verify']) {
            case 1:
                $user['taobao_verify'] = '未认证';
                break;
            case 2:
                $user['taobao_verify'] = '认证中';
                break;
            case 3:
                $user['taobao_verify'] = '认证成功';
                break;
            case 4:
                $user['taobao_verify'] = '认证失败';
                break;
        }
        //信用卡认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['credit_verify']) {
            case 1:
                $user['credit_verify'] = '未认证';
                break;
            case 2:
                $user['credit_verify'] = '认证中';
                break;
            case 3:
                $user['credit_verify'] = '认证成功';
                break;
            case 4:
                $user['credit_verify'] = '认证失败';
                break;
        }
        //学历认证1未认证2认证中3已认证
        switch ($user['edu_verify']) {
            case 1:
                $user['edu_verify'] = '未认证';
                break;
            case 2:
                $user['edu_verify'] = '认证中';
                break;
            case 3:
                $user['edu_verify'] = '认证成功';
                break;
            case 4:
                $user['edu_verify'] = '认证失败';
                break;
        }

        //支付宝认证1未认证，2认证中，3已认证，4认证失败
        switch ($user['alipay_verify']) {
            case 1:
                $user['alipay_verify'] = '未认证';
                break;
            case 2:
                $user['alipay_verify'] = '认证中';
                break;
            case 3:
                $user['alipay_verify'] = '认证成功';
                break;
            case 4:
                $user['alipay_verify'] = '认证失败';
                break;
        }

        $first = substr($user['mobile'], 0, 3);
        $end = substr($user['mobile'], 7, 4);
//        $user['user_mobile'] = $first . '****' . $end;
        $user['user_mobile']=$user['mobile'];
        //驳回
//        $refuse = M('tmp_user', '', 'KEFU')->where(['vid' => $vid])->getfield('status');
//        $refuse1 = M('anti_check_user', '', 'KEFU')->where(['vid' => $vid])->getfield('status');
        //驳回标签 implode

        $sub = M('temp_check_tag', '', 'KEFU_FK')->where(['vid' => $vid])->field('max(ctime) as t_time')->find();
        $tag = M('temp_check_tag', '', 'KEFU_FK')->where(['vid' => $vid, 'ctime' => $sub['t_time']])->getField('tag', true);
        $tags = implode('-', $tag);

        $refuse_content = M('temp_log', '', 'KEFU_FK')->where(['vid' => $vid])->field('content')->order('id desc')->limit(1)->find();
        $user['refuse_content'] = $refuse_content['content'];
        $user['refuse_status'] = '3';


        $first = substr($user['id_card'], 0, 3);
        $end = substr($user['id_card'], strlen($user['id_card']) - 9, strlen($user['id_card']));
        $user['id_card'] = $first . '******' . $end;

        $user_pic = M('user_idcard_img', '', 'USER')->where(['vid' => $user['vid']])->field('photo,photo1,photo2,front,behind')->find();
        $user['ctime'] = date('Y-m-d H:i', $user['ctime']);
        $user_contact = M('user_contact', '', 'USER')->where(['vid' => $vid])->field('relative_name,relative_mobile,r_type,other_name,other_mobile,f_type')->find();
        $w = [];
        $w['mymobile'] = array('eq', $user['mobile']);
        $w['call_other_number'] = array('eq', $user_contact['relative_mobile']);
        $contact_sum = M('mobile_callrecord_s', '', 'KF_USER_TX')->where($w)->count();
        $contact_sum2 = M('mobile_callrecord_s', '', 'KF_USER_TX')->where(['mymobile' => $user['mobile'], 'call_other_number' => $user_contact['other_mobile']])->count();
        $user_contact['contact_sum'] = $contact_sum;
        $user_contact['contact_sum2'] = $contact_sum2;

        $user['score'] = M('user_good_bad', '', 'USER')->where(['vid' => $vid])->getfield('logic_level');
        $quick_answer = M('kefu_quick_answer', '', 'KEFU')->where(['type' => '1'])->field('title,id')->select();
        $this->assign(array(
            'user' => $user,
            'user_pic' => $user_pic,
            'user_contact' => $user_contact,
            'vid' => $vid,
            'quick_answer' => $quick_answer,
            'tag' => $tags,
        ));
        $this->display();
    }

    public function survey1()
    {
        $id = I('id');
//      $vid = I('get.vid');
        $vid = M('anti_check_user', '', 'KEFU')->where(['id' => $id])->getField('vid');
        //修改调查状态
        M('anti_check_user', '', 'KEFU')->where(['vid' => $vid])->setfield('status', 7);
        $field = 'vid,mobile,sex,ctime,address,residence_time,id_card,shebao_verify,work_verify,taobao_verify,jd_verify,credit_verify,edu_verify,alipay_verify';
        $user = M('user', '', 'USER')->where(['vid' => $vid])->field($field)->find();
        //用户各种认证
        // 社保认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['shebao_verify']) {
            case 1:
                $user['shebao_verify'] = '未认证';
                break;
            case 2:
                $user['shebao_verify'] = '认证中';
                break;
            case 3:
                $user['shebao_verify'] = '认证成功';
                break;
            case 4:
                $user['shebao_verify'] = '认证失败';
                break;
        }

        //京东认证1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['jd_verify']) {
            case 1:
                $user['jd_verify'] = '未认证';
                break;
            case 2:
                $user['jd_verify'] = '认证中';
                break;
            case 3:
                $user['jd_verify'] = '认证成功';
                break;
            case 4:
                $user['jd_verify'] = '认证失败';
                break;
        }

        //工作认证 1认证2未认证
        switch ($user['work_verify']) {
            case 1:
                $user['work_verify'] = '已认证';
                break;
            case 2:
                $user['work_verify'] = '待修改';
                break;
            case 0:
                $user['work_verify'] = '未认证';
                break;
        }

        //淘宝认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['taobao_verify']) {
            case 1:
                $user['taobao_verify'] = '未认证';
                break;
            case 2:
                $user['taobao_verify'] = '认证中';
                break;
            case 3:
                $user['taobao_verify'] = '认证成功';
                break;
            case 4:
                $user['taobao_verify'] = '认证失败';
                break;
        }
        //信用卡认证 1：未认证 2：认证中 3认证成功，4.认证失败
        switch ($user['credit_verify']) {
            case 1:
                $user['credit_verify'] = '未认证';
                break;
            case 2:
                $user['credit_verify'] = '认证中';
                break;
            case 3:
                $user['credit_verify'] = '认证成功';
                break;
            case 4:
                $user['credit_verify'] = '认证失败';
                break;
        }
        //学历认证1未认证2认证中3已认证
        switch ($user['edu_verify']) {
            case 1:
                $user['edu_verify'] = '未认证';
                break;
            case 2:
                $user['edu_verify'] = '认证中';
                break;
            case 3:
                $user['edu_verify'] = '认证成功';
                break;
            case 4:
                $user['edu_verify'] = '认证失败';
                break;
        }

        //支付宝认证1未认证，2认证中，3已认证，4认证失败
        switch ($user['alipay_verify']) {
            case 1:
                $user['alipay_verify'] = '未认证';
                break;
            case 2:
                $user['alipay_verify'] = '认证中';
                break;
            case 3:
                $user['alipay_verify'] = '认证成功';
                break;
            case 4:
                $user['alipay_verify'] = '认证失败';
                break;
        }

        $first = substr($user['mobile'], 0, 3);
        $end = substr($user['mobile'], 7, 4);
        $user['user_mobile'] = $first . '****' . $end;
        //驳回
//        $refuse = M('tmp_user', '', 'KEFU')->where(['vid' => $vid])->getfield('status');
//        $refuse1 = M('anti_check_user', '', 'KEFU')->where(['vid' => $vid])->getfield('status');
        //驳回标签 implode

        $sub = M('temp_check_tag', '', 'KEFU_FK')->where(['vid' => $vid])->field('max(ctime) as t_time')->find();
        $tag = M('temp_check_tag', '', 'KEFU_FK')->where(['vid' => $vid, 'ctime' => $sub['t_time']])->getField('tag', true);
        $tags = implode('-', $tag);


        $refuse_content = M('temp_log', '', 'KEFU_FK')->where(['vid' => $vid])->field('content')->order('id desc')->limit(1)->find();
        $user['refuse_content'] = $refuse_content['content'];
        $user['refuse_status'] = '3';


        $first = substr($user['id_card'], 0, 3);
        $end = substr($user['id_card'], strlen($user['id_card']) - 9, strlen($user['id_card']));
        $user['id_card'] = $first . '******' . $end;

        $user_pic = M('user_idcard_img', '', 'USER')->where(['vid' => $user['vid']])->field('photo,photo1,photo2,front,behind')->find();
        $user['ctime'] = date('Y-m-d H:i', $user['ctime']);
        $user_contact = M('user_contact', '', 'USER')->where(['vid' => $vid])->field('relative_name,relative_mobile,r_type,other_name,other_mobile,f_type')->find();
        $w = [];
        $w['mymobile'] = array('eq', $user['mobile']);
        $w['call_other_number'] = array('eq', $user_contact['relative_mobile']);
        $contact_sum = M('mobile_callrecord_s', '', 'KF_USER_TX')->where($w)->count();
        $contact_sum2 = M('mobile_callrecord_s', '', 'KF_USER_TX')->where(['mymobile' => $user['mobile'], 'call_other_number' => $user_contact['other_mobile']])->count();
        $user_contact['contact_sum'] = $contact_sum;
        $user_contact['contact_sum2'] = $contact_sum2;

        $user['score'] = M('user_good_bad', '', 'USER')->where(['vid' => $vid])->getfield('logic_level');
        $quick_answer = M('kefu_quick_answer', '', 'KEFU')->where(['type' => '1'])->field('title,id')->select();
        $this->assign(array(
            'user' => $user,
            'user_pic' => $user_pic,
            'user_contact' => $user_contact,
            'vid' => $vid,
            'quick_answer' => $quick_answer,
            'tag' => $tags,
        ));
        $this->display();
    }

    /**
     * 用户联系人列表
     */
    public function user_contact()
    {
        $vid = I('vid');
        $n = I('page', 1);
        $num = 6;
        $page = ($n - 1) * $num;

        $user = M('user', '', 'USER')->where(['vid' => $vid])->field('mobile,vid')->find();
        //通讯录列表
        $field = "b.call_other_number,count(b.call_other_number) as num, a.mobile";
//        $tol = M('user_mobilebook', '', 'KF_USER_TX')->where(['vid' => $vid])->count();
        $count = M('user_mobilebook', '', 'KF_USER_TX')->where(['vid' => $vid])->count();
        $user_tx = M('user_mobilebook', '', 'KF_USER_TX')->query('SELECT a.name , a.mobile as call_other_number ,IFNULL(b.num,0) as num
                                                              from user_mobilebook a 
                                                              left join (SELECT call_other_number,COUNT(call_other_number) as num 
                                                                          FROM mobile_callrecord_s 
                                                                          WHERE mymobile=\'' . $user['mobile'] . '\' 
                                                                          GROUP BY call_other_number) b 
                                                                          on a.mobile=b.call_other_number 
                                                                          WHERE  a.vid=\'' . $user['vid'] . '\'
                                                                          ORDER BY num desc LIMIT ' . $page . ',' . $num);

        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $user_tx]);

    }

    /**
     * 快速回复标签
     */
    public function Quick_answer()
    {
        if (empty($_POST)) {
            $vid = I('get.vid');
            $url_type = I('get.url_type');
            $data = M('kefu_quick_answer', '', 'KEFU')->where(['type' => '1'])->field('title,id')->select();
            $this->assign(array(
                'data' => $data,
                'vid' => $vid,
                'url_type' => $url_type
            ));
            $this->display();
            die;
        }
    }

    /**
     * 回复记录
     */
    public function outbound_result()
    {
        $vid = I('vid');
        $content = I('content');
        M('user_artificial_check', '', 'KEFU')->startTrans();
        M('tmp_user_mark', '', 'KEFU')->startTrans();
        $arr = array('admin_name' => $_SESSION['name'], 'content' => $content, 'ctime' => time(), 'vid' => $vid);
        $add = M('tmp_user_mark', '', 'KEFU')->add($arr);
        $ready_time=M('user_artificial_check','','KEFU')->where(['vid' => $vid])->getfield('ready_time');
        if(empty($ready_time)){
            M('user_artificial_check', '', 'KEFU')->where(['vid' => $vid])->setfield('ready_time', time());
        }
        if ($add) {
            $examin = M('user_artificial_check', '', 'KEFU')->where(['vid' => $vid])->getField('examine_admin');
            if (empty($examin)) {
                $arr = array('research_commit_time' => time(), 'status' => 3);
            } else {
                $arr = array('research_commit_time' => time(), 'status' => 4);
            }
            $update = M('user_artificial_check', '', 'KEFU')->where(['vid' => $vid])->save($arr);
            if ($update) {
                M('user_artificial_check', '', 'KEFU')->commit();
                M('tmp_user_mark', '', 'KEFU')->commit();
            } else {
                M('user_artificial_check', '', 'KEFU')->rollback();
                M('tmp_user_mark', '', 'KEFU')->rollback();
                $this->ajax('1002', '备注失败');
                die;
            }
            add_log('反欺诈对用户' . $vid . '外呼备注ID' . $add);
            $this->ajax('1001', '备注成功');
            die;
        }
        M('user_artificial_check', '', 'KEFU')->rollback();
        M('tmp_user_mark', '', 'KEFU')->rollback();
        $this->ajax('1002', '备注失败');
        die;
    }

    /**
     * 回复记录
     */
    public function outbound_result1()
    {
        $vid = I('vid');
        $content = I('content');
        $arr = array('admin_name' => $_SESSION['name'], 'content' => $content, 'ctime' => time(), 'vid' => $vid);
        $add = M('tmp_user_mark', '', 'KEFU')->add($arr);
        M('anti_check_user', '', 'KEFU')->startTrans();
        M('tmp_user', '', 'KEFU')->startTrans();

        if ($add) {
            $check = M('tmp_user', '', 'KEFU')->where(['vid' => $vid])->field('status')->find();
            if ($check['status'] != 4 && $check['status'] != 5) {
                $anti_status = M('anti_check_user', '', 'KEFU')->where(['vid' => $vid])->getField('fk_status');
                if ($anti_status == 3) {
                    $arr = array('status' => 2, 'commit_time' => time());
                } else {
                    $arr = array('status' => 2, 'commit_time' => time(), 'fk_status' => 0);
                }
                $update1 = M('anti_check_user', '', 'KEFU')->where(['vid' => $vid])->save($arr);
                $update2 = M('tmp_user', '', 'KEFU')->where(['vid' => $vid])->setfield('status', 2);
                if ($update1 && $update2) {
                    M('anti_check_user', '', 'KEFU')->commit();
                    M('tmp_user', '', 'KEFU')->commit();
                } else {
                    M('anti_check_user', '', 'KEFU')->rollback();
                    M('tmp_user', '', 'KEFU')->rollback();
                    $this->ajax('1002', '提交失败');
                    die;
                }
                add_log('对用户' . $vid . '外呼备注ID' . $add);
            }
            $this->ajax('1001', '备注成功');
            die;
        }
        $this->ajax('1002', '备注失败');
        die;
    }

    /**
     * 回复记录 显示
     */
    public function survey_details()
    {
        $vid = I('get.vid');
        $data = M('tmp_user_mark', '', 'KEFU')->join('b join dxd_user.user a on b.vid=a.vid')->where(['b.vid' => $vid])->field('b.id,b.admin_name,b.content,b.ctime,b.vid,a.truename,a.mobile')->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            $first = substr($v['mobile'], 0, 3);
            $end = substr($v['mobile'], 7, 4);
            $data[$k]['user_mobile'] = $first . '****' . $end;
        }

        $this->assign(array(
            'data' => $data,
            'vid' => $vid,
        ));
        $this->display();
    }

    /**
     *回复记录  未提交
     */
    public function outbound_result_no()
    {
        $vid = I('vid');
        $content = I('content');
        $arr = array('admin_name' => $_SESSION['name'], 'content' => $content, 'ctime' => time(), 'vid' => $vid);
        $this->page = I('page');
        M('user_artificial_check', '', 'KEFU')->where(['vid' => $vid])->setfield('ready_time', time());
        $add = M('tmp_user_mark', '', 'KEFU')->add($arr);
        if ($add) {
            add_log('反欺诈对用户' . $vid . '外呼备注ID' . $add);
            $this->ajax('1001', '备注成功');
            die;
        }
        $this->ajax('1002', '备注失败');
        die;
    }
    /**
     * 编辑内容
     */
    public function edit_content()
    {
        $id = I('id');
        $content = I('edit_content');
        if (empty($content)) {
            $this->ajax('1002', '修改失败');
            die;
        }
        $info = M('tmp_user_mark', '', 'KEFU')->where(['id' => $id])->setfield('content', $content);
        if ($info) {
            add_log('反欺诈修改外呼记录' . $id);
            $this->ajax('1001', '修改成功');
            die;
        }
        $this->ajax('1002', '修改失败');
        die;
    }
    /**
     * http请求
     * @param  string $url 请求地址
     * @param  boolean|string|array $params 请求数据
     * @param  integer $ispost 0/1，是否post
     * @param  array $header
     * @param  $verify 是否验证ssl
     * return string|boolean          出错时返回false
     */
    function http($url, $params, $ispost = 1, $header = [], $verify = false)
    {
        $httpInfo = array();
        $ch = curl_init();
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        //忽略ssl证书
        if ($verify === true) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        } else {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if (is_array($params)) {
                $params = http_build_query($params);
            }
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === FALSE) {
            trace("cURL Error: " . curl_errno($ch) . ',' . curl_error($ch), 'error');
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
            trace($httpInfo, 'error');
            return false;
        }
        curl_close($ch);
        return $response;
    }

    //转为预审
    public function turn_kf()
    {
        $vid = I('vid');
        $has = M('anti_check_user', '', 'KEFU')->where(['vid' => $vid])->find();//是否存在已经调查过
        //随机分配给online客服
        $where = [];
        $where['agent_status'] = array('in', array(1, 2, 3, 0));
        $where['status'] = array('eq', 1);
        $where['role_id'] = array('eq', 2);
        $where['part_id'] = array('eq', 0);
        $where['out_kf_id'] = array('eq', 0);
        $admins = M('admin', '', 'KEFU')->where($where)->getField('id', true);
        if (!empty($admins)) {
            $ida = array_rand($admins, 1);
            $id = $admins[$ida];
        } else {
            $id = 101;
        }
        if (!empty($has)) {
            $set = array('status' => 9, 'out_kf_id' => $id, 'update_time' => time());
            $update = M('anti_check_user', '', 'KEFU')->where(['vid' => $vid])->save($set);
        } else {
            $add_arr = array('vid' => $vid, 'type' => 5, 'out_kf_id' => $id, 'status' => 9, 'ctime' => time());
            $add = M('anti_check_user', '', 'KEFU')->add($add_arr);
        }
        if ($add || $update) {
            //加入备注
            $arr = array('vid' => $vid, 'content' => '反欺诈驳回重预审', 'admin_id' => $_SESSION['id'], 'ctime' => time(), 'type' => '1');
            M('work_cert_mark', '', 'KEFU')->add($arr);
            M('tmp_user', '', 'KEFU')->where(['vid' => $vid])->setField('status', 6);
            add_log('反欺诈转为预审' . $vid);
            $this->ajaxReturn(['code' => '1001', 'result' => '成功']);
        } else {
            add_log('tmp_user用户添加失败' . $vid);
            $this->ajaxReturn(['code' => '1002', 'result' => '失败']);
        }
    }
}
